package com.javasm.service.B2cOrderService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.orderCriteria.OrderGoodsCriteria;
import com.javasm.domin.entity.orderEntity.B2cOrderGoods;
import com.javasm.domin.vo.orderVo.OrderGoodVo;
import com.javasm.mapper.B2cOrderMapper.OrderGoodsMapper;
import com.javasm.service.B2cOrderService.OrderGoodsService;
import com.javasm.transfer.orderTransfer.OrderGoodsTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lys
 * @since 2021-11-20
 */
@Service
@RequiredArgsConstructor
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, B2cOrderGoods> implements OrderGoodsService {
    private final OrderGoodsMapper orderGoodsMapper;
    private final OrderGoodsTransfer orderGoodsTransfer;

    /**
     * 根据订单id得到商品详细信息
     *
     * @param orderGoodsCriteria
     * @return
     */
    @Override
    public List<B2cOrderGoods> findGoodByOrderId(OrderGoodsCriteria orderGoodsCriteria) {
        LambdaQueryWrapper<B2cOrderGoods> lambda = new QueryWrapper<B2cOrderGoods>().lambda();
        if (!StringUtils.isEmpty(orderGoodsCriteria.getOrderId())) {
            lambda.eq(B2cOrderGoods::getOrderId, orderGoodsCriteria.getOrderId());
        }
        List<B2cOrderGoods> b2cOrderGoods = orderGoodsMapper.selectList(lambda);
        return b2cOrderGoods;
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public PageResult<OrderGoodVo> findPage(OrderGoodsCriteria orderGoodsCriteria) {
        PageHelper.startPage(orderGoodsCriteria.getCurrentPage(), orderGoodsCriteria.getPageSize());
        B2cOrderGoods b2cOrderGoods = new B2cOrderGoods();
        QueryWrapper<B2cOrderGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*,sum(total) sales");
        queryWrapper.groupBy("goods_id");

        if (!StringUtils.isEmpty(orderGoodsCriteria.getTypeId())) {
            queryWrapper.eq("type_id", orderGoodsCriteria.getTypeId());
        }
        if (!StringUtils.isEmpty(orderGoodsCriteria.getColorId()) && orderGoodsCriteria.getColorId() != 0) {
            queryWrapper.eq("color_id", orderGoodsCriteria.getColorId());
//
        }
        if (!StringUtils.isEmpty(orderGoodsCriteria.getOrderProductName())) {
            queryWrapper.like("order_product_name", orderGoodsCriteria.getOrderProductName());
//
        }
        if (!StringUtils.isEmpty(orderGoodsCriteria.getBrandId())) {
//            lambda.eq(B2cOrderGoods::getBrandId, orderGoodsCriteria.getBrandId());
            queryWrapper.eq("brand_id", orderGoodsCriteria.getBrandId());
        }
        if (!StringUtils.isEmpty(orderGoodsCriteria.getStartTime())){
            queryWrapper.between("create_time",orderGoodsCriteria.getStartTime(),orderGoodsCriteria.getEndTime());
        }

        List<B2cOrderGoods> b2cOrderGoodsList = orderGoodsMapper.selectList(queryWrapper);

        PageInfo<B2cOrderGoods> pageInfo = new PageInfo<>(b2cOrderGoodsList);
        //转vo
        List<OrderGoodVo> orderGoodVoList = orderGoodsTransfer.setString(b2cOrderGoodsList);

        return new PageResult<OrderGoodVo>(pageInfo.getTotal(), orderGoodVoList);
    }

    /**
     * 根据订单id批量查询
     *
     * @param orderGoodsCriteria
     * @return
     */
    @Override
    public List<OrderGoodVo> findOrderGoodByOrderIdList(OrderGoodsCriteria orderGoodsCriteria) {
        LambdaQueryWrapper<B2cOrderGoods> lambda = new QueryWrapper<B2cOrderGoods>().lambda();
        if (!StringUtils.isEmpty(orderGoodsCriteria.getOrderIdList())) {
            lambda.in(B2cOrderGoods::getOrderId, orderGoodsCriteria.getOrderIdList());

        }
        List<B2cOrderGoods> b2cOrderGoods = orderGoodsMapper.selectList(lambda);
        //转vo 转字符
        List<OrderGoodVo> orderGoodVoList = orderGoodsTransfer.setString(b2cOrderGoods);
        return orderGoodVoList;
    }


    /**
     * 销量统计
     *
     * @param orderGoodsCriteria
     * @return
     */
    @Override
    public PageResult<OrderGoodVo> orderGoodsTotal(OrderGoodsCriteria orderGoodsCriteria) {
//        PageHelper.startPage(orderGoodsCriteria.getCurrentPage(), orderGoodsCriteria.getPageSize());
//        LambdaQueryWrapper<B2cOrderGoods> lambda = new QueryWrapper<B2cOrderGoods>().lambda();
//
////        if (!StringUtils.isEmpty(orderGoodsCriteria.getTypeId())
////                && !StringUtils.isEmpty(orderGoodsCriteria.getTypeId())
////                && !StringUtils.isEmpty(orderGoodsCriteria.getOrderProductName())
////                && !StringUtils.isEmpty(orderGoodsCriteria.getBrandId())
////                && !StringUtils.isEmpty(orderGoodsCriteria.getColorId())) {
//        if (orderGoodsCriteria != null) {
//            List<B2cOrderGoods> b2cOrderGoodsList1 = orderGoodsMapper.OrderGoodCri(orderGoodsCriteria.getOrderProductName(), orderGoodsCriteria.getTypeId(), orderGoodsCriteria.getBrandId(), orderGoodsCriteria.getColorId());
////            List<B2cOrderGoods> b2cOrderGoodsList = orderGoodsMapper.orderGoodsTotalByGoodName(orderGoodsCriteria.getOrderProductName());
//            PageInfo<B2cOrderGoods> pageInfo = new PageInfo<>(b2cOrderGoodsList1);
//            List<OrderGoodVo> orderGoodVoList = orderGoodsTransfer.setString(b2cOrderGoodsList1);
//            return new PageResult<OrderGoodVo>(pageInfo.getTotal(), orderGoodVoList);
//        }
//        //颜色id
//        if (!StringUtils.isEmpty(orderGoodsCriteria.getColorId()) && orderGoodsCriteria.getColorId() != 0) {
//            List<B2cOrderGoods> b2cOrderGoodsList = orderGoodsMapper.OrderGoodByColorId(orderGoodsCriteria.getColorId());
//            PageInfo<B2cOrderGoods> pageInfo = new PageInfo<>(b2cOrderGoodsList);
//            List<OrderGoodVo> orderGoodVoList = orderGoodsTransfer.setString(b2cOrderGoodsList);
//            return new PageResult<OrderGoodVo>(pageInfo.getTotal(), orderGoodVoList);
//        }
//        if (!StringUtils.isEmpty(orderGoodsCriteria.getOrderProductName())) {
//            List<B2cOrderGoods> b2cOrderGoodsList = orderGoodsMapper.orderGoodsTotalByGoodName(orderGoodsCriteria.getOrderProductName());
//            PageInfo<B2cOrderGoods> pageInfo = new PageInfo<>(b2cOrderGoodsList);
//            List<OrderGoodVo> orderGoodVoList = orderGoodsTransfer.setString(b2cOrderGoodsList);
//            return new PageResult<OrderGoodVo>(pageInfo.getTotal(), orderGoodVoList);
////            lambda.like(B2cOrderGoods::getOrderProductName, orderGoodsCriteria.getOrderProductName());
//        }
//        if (!StringUtils.isEmpty(orderGoodsCriteria.getTypeId())) {
//            List<B2cOrderGoods> b2cOrderGoodsList = orderGoodsMapper.OrderGoodByTypeId(orderGoodsCriteria.getTypeId());
//            PageInfo<B2cOrderGoods> pageInfo = new PageInfo<>(b2cOrderGoodsList);
//            List<OrderGoodVo> orderGoodVoList = orderGoodsTransfer.setString(b2cOrderGoodsList);
//            return new PageResult<OrderGoodVo>(pageInfo.getTotal(), orderGoodVoList);
//        }
//        if (!StringUtils.isEmpty(orderGoodsCriteria.getBrandId())) {
//            lambda.eq(B2cOrderGoods::getBrandId, orderGoodsCriteria.getBrandId());
//        }
//        List<B2cOrderGoods> b2cOrderGoodsList = orderGoodsMapper.orderGoodsTotal(lambda);
//
//        PageInfo<B2cOrderGoods> pageInfo = new PageInfo<>(b2cOrderGoodsList);
//        //转vo
//        List<OrderGoodVo> orderGoodVoList = orderGoodsTransfer.setString(b2cOrderGoodsList);

        return null;
    }




}
